<?php

// +----------------------------------------------------------------------

// | Fanwe 多语商城建站系统 (Build on ThinkPHP)

// +----------------------------------------------------------------------

// | Copyright (c) 2009 http://www.fanwe.com All rights reserved.

// +----------------------------------------------------------------------

// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )

// +----------------------------------------------------------------------

// | Author: 云淡风轻(97139915@qq.com)

// +----------------------------------------------------------------------



//商品

define('NAME_REQUIRE',L('NAME_REQUIRE'));

define('URL_REQUIRE',L('URL_REQUIRE'));

define('MARKET_PRICE_MUST_BE_NUM',L('MARKET_PRICE_MUST_BE_NUM'));

define('SHOP_PRICE_MUST_BE_NUM',L('SHOP_PRICE_MUST_BE_NUM'));

define('END_TIME_ERROR',L('END_TIME_ERROR'));

define('SORT_MUST_BE_NUM',L('SORT_MUST_BE_NUM'));



class GoodsModel extends MultiLangModel {

	protected $_validate = array(

			array('name','require',NAME_REQUIRE),

			array('url','require',URL_REQUIRE),

			array('market_price','is_numeric',MARKET_PRICE_MUST_BE_NUM,2,'function'), 

			array('shop_price','is_numeric',SHOP_PRICE_MUST_BE_NUM,2,'function'), 

			array('sort','is_numeric',SORT_MUST_BE_NUM,2,'function'), 
			
			array('end_time','checkDateFormat',END_TIME_ERROR,2,'function'),

		);

		

	protected $_auto = array ( 			   

		array('end_time','localStrToTime',3,'function'),
		array('begin_time','localStrToTime',3,'function'),
		array('update_time','gmtTime',3,'function')

	);
	
	public function getGoodsPartitions()
	{
		$now_time = gmtTime();
		$date = D('Goods')->min('end_time');
		$min_year = toDate($date,'Y');
		$min_month = toDate($date,'m');
		$max_year = toDate($now_time,'Y');
		$max_month = toDate($now_time,'m');
		$time_arr = array();
		$partitions = array();

		for($year = $min_year;$year <= $max_year;$year++)
		{
			$mmonth = 12;
			$month = 1;

			if($year == $max_year)
				$mmonth = $max_month;

			if($year == $min_year)
				$month = $min_month;

			for($month;$month <= $mmonth;$month++)
			{
				$next_year = $year;
				$next_month = $month + 1;

				if($month == 12)
				{
					$next_year++;
					$next_month = 1;
				}
				$time_arr[localStrToTime($year.'-'.$month.'-01 00:00:00')] = localStrToTime($next_year.'-'.$next_month.'-01 00:00:00');
			}
		}

		$index = 1;
		$count = count($time_arr);
		foreach($time_arr as $min_time => $max_time)
		{
			if($max_time > $now_time)
				$max_time = $now_time;

			$where = " gk.end_time >= $min_time AND gk.end_time < $max_time";
			$partitions[$min_time]['date'] = toDate($min_time,'Y年m月');
			$partitions[$min_time]['min_date'] = $min_time;
			$partitions[$min_time]['max_date'] = $max_time;
			$partitions[$min_time]['where'] = $where;
			$index++;
		}

		krsort($partitions);

		return $partitions;
	}
}

?>